package com.atguigu.service;

import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.entity.RolePermission;
import com.atguigu.helper.PermissionHelper;
import com.atguigu.service.PermissionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DubboService
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    protected BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    @Override
    public List<Map<String, Object>> findPermissionByRoleId(Long roleId) {
        //1.查询出所有权限
        List<Permission> allPermissionList = permissionDao.findAll();
        //2.每一个Permission应该映射成一个Map,Map中包含五个键值对
        //声明一个集合用来存储Permission
        List<Map<String,Object>> zNodes = new ArrayList<>();
        //查询当前角色已分配的权限id集合
        List<Long> assignPermissionIdList = rolePermissionDao.findPermissionIdListByRoleId(roleId);
        for (Permission permission : allPermissionList) {
            //2.1创建Map
            Map<String,Object> zNode = new HashMap<>();
            //2.2往Map中设置5个键值对
            zNode.put("id", permission.getId());
            zNode.put("pId", permission.getParentId());
            zNode.put("name", permission.getName());
            //设置open，要判断当前权限是否还有子节点，如果有则设置open属性为true
            //如何判断?以当前节点的id作为parentId来查询List<Permission>，如果查到的集合不为空，则当前节点还有子节点
            List<Permission> children = permissionDao.findByParenId(permission.getId());
            if (!CollectionUtils.isEmpty(children)) {
                zNode.put("open", true);
            }
            //设置checked属性，判断当前permission是否非配给了当前角色，如果分配了就设置checked为true，否则为false
           /* if (assignPermissionIdList.contains(permission.getId())) {
                zNode.put("checked", true);
            } else {
                zNode.put("checked", false);
            }*/
            zNode.put("checked", assignPermissionIdList.contains(permission.getId()));
            //将zNode添加到zNodes
            zNodes.add(zNode);
        }
        return zNodes;
    }

    @Override
    public void saveRolePermission(Long roleId, List<Long> permissionIds) {
        //1.删除当前角色已分配的所有权限
        rolePermissionDao.deleteByRoleId(roleId);
        //2.新增分配权限
        if (!CollectionUtils.isEmpty(permissionIds)) {
            for (Long permissionId : permissionIds) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                rolePermissionDao.insert(rolePermission);
            }
        }

    }

    @Override
    public List<Permission> findMenuPermissionByAdminId(Long adminId) {
        //1.查询出当前用户的所有权限
        List<Permission> permissionList = permissionDao.findMenuPermissionByAdminId(adminId);
        //2.构建父子菜单
        List<Permission> menu = PermissionHelper.build(permissionList);
        return menu;
    }

    @Override
    public List<Permission> findAllMenu() {
        //全部权限列表
        List<Permission> permissionList = permissionDao.findAll();
        if(CollectionUtils.isEmpty(permissionList)) return null;

        //构建树形数据,总共三级
        //把权限数据构建成树形结构数据
        List<Permission> result = PermissionHelper.build(permissionList);
        return result;
    }

    @Override
    public List<String> findPermissionCodeListByAdminId(Long adminId) {
        List<Permission> permissionList = null;
        if (adminId == 1) {
            permissionList = permissionDao.findAll();
        } else {
            //1.根据用户id查询所有权限(如果是超管)
            permissionList = permissionDao.findMenuPermissionByAdminId(adminId);
        }

        //2.过滤出三级菜单、并且将permission对象映射成String
        List<String> permissionCodeList = null;
        if (!CollectionUtils.isEmpty(permissionList)) {
            permissionCodeList = permissionList.stream()
                    .filter(permission -> permission.getType() == 2)
                    .map(Permission::getCode)
                    .collect(Collectors.toList());
        }
        return permissionCodeList;
    }


}
